package edu.ipfw.cs590.cheat.services.rest;
/*
 * Course:    CS 590 Web Services
 * Semseter:  Spring 2009
 * Professor: Sedlemeyer
 * Project:   CHEAT (Code Help Educational Assignment Tool)
 * Due Date:  May 1, 2009
 * Team:      Sarcasm
 *            (Baker, Jason; Mitchell, Robin; Shie, Keith)
 */

import edu.ipfw.cs590.cheat.services.AssignmentWrapper;
import edu.ipfw.cs590.cheat.services.Keyword;
import edu.ipfw.cs590.cheat.services.mgmt.AssignmentManagementServer;
import edu.ipfw.cs590.cheat.services.mgmt.faults.AMSException;
import edu.ipfw.cs590.cheat.services.utils.AssignmentMarshaller;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.w3c.dom.Document;
import org.web_cat._1_0.assignment_sharing.Assignment;

/**
 * REST Web Service
 *
 * @author SafarTimuro
 */

@Path("/Assignments/{operation}/{search}")
public class RESTServices extends AssignmentManagementServer {
    @Context
    private UriInfo context;

    /** Creates a new instance of RESTServices */
    public RESTServices() {}


    @GET
    @Produces("application/xml")
    public String getXml(@PathParam("operation")String op, @PathParam("search") String searchString)
    {
        System.out.println("getXml called");
        {
            try
            {
                List<AssignmentWrapper> assignmentList = new ArrayList<AssignmentWrapper>();

                if (op.compareToIgnoreCase("course") == 0)
                    assignmentList = this.doFindAssignmentByCourse(searchString);
                else if (op.compareToIgnoreCase("language") == 0)
                    assignmentList = this.doFindAssignmentByLanguage(searchString);
                else if (op.compareToIgnoreCase("author") == 0)
                    assignmentList = this.doFindAssignmentByAuthor(searchString);
                else if (op.compareToIgnoreCase("keyword") == 0)
                {
                    StringTokenizer tokenizer = new StringTokenizer(searchString, ":");
                    ArrayList<Keyword> keywords = new ArrayList<Keyword>();
                    while ( tokenizer.hasMoreTokens())
                    {
                        String s = tokenizer.nextToken();
                        System.out.println("->>> " + s);
                        keywords.add(new Keyword(s));
                    }
                    assignmentList = this.doFindAssignmentByKeyword(keywords);
                }

                List<Document> documentList = this.buildDOMList(assignmentList);
                StringBuffer sBuffer = new StringBuffer();
                sBuffer.append("<cheat>");
                if (assignmentList.size() > 0)
                {
                    for (Document d : documentList)
                    {
                        sBuffer.append(new AssignmentMarshaller(d).getAssignmentString());
                    }
                }
                sBuffer.append("</cheat>");
                return sBuffer.toString();
            }
            catch (AMSException ex)
            {
                Logger.getLogger(RESTServices.class.getName()).log(Level.SEVERE, null, ex);
                return "";
            } 
            finally
            {
            }
        }
    }


    @PUT
    @Consumes("text/html")
    public void putHtml(String content)
    {
    }
}
